<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="1000"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
		<a-form-item label="排序" field="sort_order">
		  <a-input-number v-model="formData.sort_order" placeholder="请输入排序" />
		</a-form-item>
      <a-form-item label="政策/资讯主图路径" field="policy_image">
        <sa-upload-image v-model="formData.policy_image" :limit="3" :multiple="false" />
      </a-form-item>
      <a-form-item label="政策/资讯模块" field="policy_module">
        <a-select
            v-model="formData.policy_module"
            :options="postData"
             @change="handleModuleChange"
            allow-clear
            placeholder="请选择政策/资讯模块" />
      </a-form-item>
      <a-form-item label="政策/资讯类别" field="policy_category">
        <a-select
            v-model="formData.policy_category"
            :options="categoryData"
            allow-clear
            placeholder="政策/资讯类别" />
      </a-form-item>
      <a-form-item label="是否置顶" field="is_top">
        <sa-switch v-model="formData.is_top" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
      <a-form-item label="是否推荐" field="is_recommend">
        <sa-switch v-model="formData.is_recommend" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
      <a-form-item label="政策/资讯标题" field="policy_title">
        <a-input v-model="formData.policy_title" placeholder="请输入政策/资讯标题" />
      </a-form-item>
      <a-form-item label="发布单位" field="issuing_unit">
        <a-input v-model="formData.issuing_unit" placeholder="请输入发布单位" />
      </a-form-item>
      <a-form-item label="政策/资讯详情" field="policy_details">
        <ma-wangEditor v-model="formData.policy_details" :height="400" />
      </a-form-item>
	 
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/policy'
import commonApi from '@/api/common'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const postData = ref([])
const categoryData = ref([])

let title = computed(() => {
  return '政策列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  policy_id: null,
  sort_order: 0,
  policy_image: '',
  policy_module: '',
  policy_category: '',
  is_top: 0,
  is_recommend: 0,
  policy_title: '',
  issuing_unit: '',
  policy_details: '',
  lang_code: ''
})

// 验证规则
const rules = {
  lang_code: [{ required: true, message: '语言代码，如zh-CN、en-US等必需填写' }],
  policy_module: [{ required: true, message: '政策/资讯模块必需填写' }],
  policy_category: [{ required: true, message: '政策/资讯类别必需填写' }],
  policy_title: [{ required: true, message: '政策/资讯标题必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
   const postResp = await api.getDictData()
   postData.value = postResp.data
   const category = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate', {
     classification_type: formData.policy_module
   })
   categoryData.value = category.data
}
const handleModuleChange = async (value) => {
	console.log(value)
  formData.policy_category = '' // 清空类别选择
  try {
    // 根据选中的 policy_module 获取对应的 category 数据
    const category = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate', {
      classification_type: value
    })
    categoryData.value = category.data
  } catch (error) {
    console.error('获取类别数据失败', error)
    Message.error('获取类别数据失败')
  }
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.policy_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.policy_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
